package com.sskj.givecoin.common.decoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.givecoin.common.App;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SectionDividerItemDecoration extends RecyclerView.ItemDecoration {


    //分割线画笔
    private Paint mPaint;

    Map<Integer, String> maps = new HashMap<Integer, String>();
    //每一块级所占的条目数
    private List<Integer> sections = new ArrayList<>();
    //头部高度
    private int headHeight;
    //块级间隔高度
    private int sectionHeight = (int) RudenessScreenHelper.dp2px(App.INSTANCE, 50);
    //分割线高度
    private int dividerHeight;
    //分割线距离左边距离
    private int leftPadding = 0;
    //分割线距离右边距离
    private int rightPadding = 0;
    //最后一个条目下边距
    private int lastBootomHeght = 0;
    private boolean hasMore = false;

    public SectionDividerItemDecoration setHeadHeight(int headHeight) {
        this.headHeight = headHeight;
        return this;
    }

    public SectionDividerItemDecoration setLeftPadding(int leftPadding) {
        this.leftPadding = leftPadding;
        return this;
    }

    public SectionDividerItemDecoration setRightPadding(int rightPadding) {
        this.rightPadding = rightPadding;
        return this;
    }

    public SectionDividerItemDecoration setLastBootomHeght(int lastBootomHeght) {
        this.lastBootomHeght = lastBootomHeght;
        return this;
    }

    public SectionDividerItemDecoration setSectionHeight(int sectionHeight) {
        this.sectionHeight = sectionHeight;
        return this;
    }

    public SectionDividerItemDecoration setDividerHeight(int dividerHeight) {
        this.dividerHeight = dividerHeight;
        return this;
    }

    public SectionDividerItemDecoration(Context context, int... sections) {
        int num = 0;
        for (int section : sections) {
            this.sections.add(num += section);
        }
        mPaint = new Paint();

    }

    public SectionDividerItemDecoration(Context context) {
        mPaint = new Paint();

    }

    public SectionDividerItemDecoration setDividerColor(int dividerColor) {
        mPaint.setColor(dividerColor);
        return this;
    }

    public SectionDividerItemDecoration setTitle(String... titles) {
        maps.put(0, titles[0]);
        for (int i = 1; i < titles.length; i++) {
            maps.put(sections.get(i - 1), titles[i]);
        }
        return this;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = parent.getChildAt(i);
            int position = parent.getChildAdapterPosition(view);
            if (position == parent.getAdapter().getItemCount() - 1 && hasMore)
                continue;

            for (Integer section : sections) {
                if (section == position) {

                    c.drawRect(left + leftPadding, view.getTop() - sectionHeight, right - rightPadding, view.getTop(), mPaint);
                }
            }
//            if (sections.contains(position)) {
//
//            }
//            if (position == 0) {
//                if (!TextUtils.isEmpty(maps.get(0))) {
//                    c.drawText(maps.get(0), 80, view.getTop() - 20, mPaint);
//                }
//            }
//            if (sections.contains(position)) {
//                if (!TextUtils.isEmpty(maps.get(position))) {
//                    c.drawText(maps.get(position), 80, view.getTop() - 20, mPaint);
//                }
//            }
        }
    }

    /**
     * 设置item分割线的size
     *
     * @param outRect outRect
     * @param view    view
     * @param parent  parent
     * @param state   state
     */
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        int top = dividerHeight;
        int bottom = dividerHeight;
        if (position == 0) {
            top += headHeight;
        } else {
            for (Integer section : sections) {
                if (section == position) {
                    top += sectionHeight;
                }
            }

        }

        if (sections.size() > 0 && sections.get(sections.size() - 1) == position + 1 && hasMore) {
            bottom += lastBootomHeght;
        }
        outRect.set(0, top, 0, bottom);
    }
}